import * as dotenv from 'dotenv'
import TelegramBot from 'node-telegram-bot-api'
// import express from 'express'
// import bodyParser from 'body-parser'
// import cors from 'cors'

import { Configuration,OpenAIApi } from 'openai'
dotenv.config()

const { token, apiKey,orgID,model, group_name } = process.env
const prefix = group_name ? '/' + group_name : '/gpt'
const bot = new TelegramBot(token, { polling: true });
console.log(new Date().toLocaleString(), '--Bot has been started...');

//const api = new ChatGPTAPI({ apiKey })
const configuration=new Configuration({
  organization:orgID,
  apiKey:apiKey
});
const openai=new OpenAIApi(configuration);
bot.on('text', async (msg) => {
  console.log(new Date().toLocaleString(), '--Received message from id:', msg.chat.id, ':', msg.text);
  await msgHandler(msg);
});


// const completion=await openai.createChatCompletion({
//   model:model,
//   messages:[
//     {role:"user",content:"hello"}
//   ]
// });




async function msgHandler(msg) {
  if (typeof msg.text !== 'string' || ((msg.chat.type === 'group' || msg.chat.type === 'supergroup') && !msg.text.startsWith(prefix))) {
    return;
  }
  switch (true) {
    case msg.text.startsWith('/start'):
      await bot.sendMessage(msg.chat.id, '👋你好！很高兴能与您交谈。有什么我可以帮您的吗？');
      break;
    case msg.text.length >= 2:
      await chatGpt(msg);
      break;
    default:
      await bot.sendMessage(msg.chat.id, '😭我不太明白您的意思。');
      break;
  }
}

async function chatGpt(msg) {
  try {
    const tempId = (await bot.sendMessage(msg.chat.id, '🤔正在组织语言，请稍等...', {
      reply_to_message_id: msg.message_id
    })).message_id;
    bot.sendChatAction(msg.chat.id, 'typing');
    
    const completion=await openai.createChatCompletion({
      model:model,
      messages:[
        {role:"user",content:msg.text.replace(prefix, '')}
      ]
    });

    //console.log(new Date().toLocaleString(), '--AI response to <', msg.text, '>:', completion.data.choices[0].message);
    await bot.editMessageText(completion.data.choices[0].message.content, { parse_mode: 'Markdown', chat_id: msg.chat.id, message_id: tempId });
  
  
  
  
  } catch (err) {
    console.log('Error:', err)
    await bot.sendMessage(msg.chat.id, '😭出错了，请稍后再试；如果您是管理员，请检查日志。');
    throw err
  }
}